import { Effect, Reducer, Subscription } from 'umi';
import {getMailList,filterMailList} from '../services/index'
export interface MailModelState {
  mailList: [];
}

export interface MailModelType {
  namespace: 'mail';
  state: MailModelState;
  effects: {
    getMailList: Effect;
    filterMailList: Effect;
  };
  reducers: {
    save: Reducer<MailModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}

const BenchModel: MailModelType = {
  namespace: 'mail',

  state: {
    mailList: []
  },

  effects: {
    *getMailList({ payload ,current}, { call, put }) {  //获取邮箱列表
        let result = yield getMailList(current)
        yield put({
            type:'save',
            payload:{
              mailList:result.data
            }
        })
    },
    *filterMailList({ payload ,obj}, { call, put }) {  //过滤邮箱列表
        let result = yield filterMailList(obj)
        yield put({
            type:'save',
            payload:{
              mailList:result.data
            }
        })
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  }
};

export default BenchModel;